<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button v-has="userPermission.add" size="small" type="primary" @click="showAdd = true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="头像" sortable="" width="100" @click="headImg">
            <template v-slot="{row}">
              <img
                v-imgError="require('@/assets/common/head.jpg')"
                :src="row.staffPhoto"
                alt=""
                style="width:100%; border-radius:50%"
                @click="headImg(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column
            label="聘用形式"
            sortable=""
            prop="formOfEmployment"
            :formatter="formatEmployment"
          />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry" />
          <el-table-column label="状态" sortable="" prop="" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/' + row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="showAssignDialog(row.id)">角色</el-button>
              <el-button v-if="checkPermission(userPermission.remove)" type="text" size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="prev, pager, next,total"
            :current-page.sync="page.page"
            :total="page.total"
            :page-size="page.size"
            @current-change="getEmployeesList"
          />
        </el-row>
      </el-card>
    </div>
    <add-employees :visible.sync="showAdd" />
    <assign-role :visible.sync="showAssignRole" :employee-id="employeeId" />
    <el-dialog
      title="二维码"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employess'
import PermissionEnum from '@/constant/permission'
import AssignRole from './components/AssignRole.vue'
import { delEmployees, getEmployeesList } from '@/api/employees'
import AddEmployees from '@/views/employees/components/AddEmployees.vue'
// import headImg from '@/assets/common/head.jpg'
import QRCode from 'qrcode'
import PermissionMixin from '@/mixin/permission'
export default {
  components: { AddEmployees, AssignRole },
  mixins: [PermissionMixin],
  data() {
    return {
      userPermission: PermissionEnum.user,
      // headImg: headImg,
      page: {
        page: 1,
        size: 10,
        total: 0
      },

      list: [],
      dialogVisible: false,
      showAssignRole: false,
      showAdd: false,
      employeeId: ''
    }
  },

  created() {
    this.getEmployeesList()
  },

  methods: {
    async getEmployeesList() {
      const { rows, total } = await getEmployeesList(this.page)
      this.page.total = total
      this.list = rows
    },
    // formatEmployment(row, column, cellValue, index) {
    //   // cellValue 当前这一格的数据
    //   if (cellValue === 1) return '正式'
    //   if (cellValue === 2) return '非正式'
    //   return '未知'
    // }
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find((item) => item.id === cellValue)
      return obj ? obj.value : '未知'
    },

    async delFn(id) {
      try {
        await this.$confirm('燕子~，燕子~，没有你我可怎么活啊~~')
        await delEmployees(id)
        this.$message.success('删除成功了哦~~')
        this.getEmployeesList()
      } catch (e) {
        console.log(e)
      }
    },

    async exportExcel() {
      // 动态引入
      const excel = await import('@/vendor/Export2Excel')
      const { rows } = await getEmployeesList({ page: 1, size: this.page.total })

      const { exportExcelMapPath } = EmployeeEnum
      const header = Object.keys(exportExcelMapPath)
      const data = rows.map(item => {
        return header.map(ele => {
          const val = item[exportExcelMapPath[ele]]
          if (ele === '聘用形式') {
            return EmployeeEnum.hireType.find(t => t.id === val)?.value || '未知'
          }
          return val
        })
      })
      console.log(data)
      excel.export_json_to_excel({
        header: header,
        data: data,
        filename: '守护最好的坤坤'
      })
    },

    headImg(url) {
      if (url) {
        this.dialogVisible = true
        this.$nextTick(() => {
          QRCode.toCanvas(this.$refs.myCanvas, url)
        })
      }
    },
    showAssignDialog(id) {
      this.showAssignRole = true
      this.employeeId = id
    }
  }
}
</script>

<style scoped lang="less"></style>
